﻿Imports System.Data.OleDb

Public Class Profile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Only show the edit profile tab and edit/delete recipe, delete comments and delete ratings functionalities if the user is looking at their profile
        If Request.Params("User") = User.Identity.Name Then
            TabPanel3.Visible = True
            grd_ActivityRecipes.Columns(2).Visible = True
            grd_ActivityRecipes.Columns(3).Visible = True
            grd_ActivityComments.Columns(2).Visible = True
            grd_ActivityRatings.Columns(2).Visible = True
        Else
            TabPanel3.Visible = False
            grd_ActivityRecipes.Columns(2).Visible = False
            grd_ActivityRecipes.Columns(3).Visible = False
            grd_ActivityComments.Columns(2).Visible = False
            grd_ActivityRatings.Columns(2).Visible = False
        End If

        'The if not ispostback = if I got to this page NOT from this page, then load saved data
        If Not IsPostBack Then

            Dim prPhotoPath As String = ""
            Dim ctryID As String = ""

            'Load current data from the Profile table
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)
            Dim SqlString As String = "SELECT * from PROFILE WHERE USER_UNAME=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()

            'Since we need to get back a record - establish data reader
            Dim dr As OleDbDataReader = cmd.ExecuteReader
            If dr.HasRows Then
                dr.Read()
                'fill in the textboxes and profile with the data returned (as long as there is something - not null)
                If Not IsDBNull(dr("USER_FNAME")) Then tb_PrFName.Text = dr("USER_FNAME")
                If Not IsDBNull(dr("USER_LNAME")) Then tb_PrLName.Text = dr("USER_LNAME")
                If Not IsDBNull(dr("CTRY_ID")) Then ddl_PrCountry.SelectedValue = dr("CTRY_ID")
                If Not IsDBNull(dr("USER_DOB")) Then tb_PrDOB.Text = dr("USER_DOB")
                If Not IsDBNull(dr("USER_EMAIL")) Then tb_PrEmail.Text = dr("USER_EMAIL")
                If Not IsDBNull(dr("USER_BIO")) Then tb_PrBio.Text = dr("USER_BIO")
                If Not IsDBNull(dr("USER_PHOTO")) Then prPhotoPath = dr("USER_PHOTO")
            End If
            oleDbConn.Close()
            oleDbConn.Dispose()

            'Get the Comments Activity Statistics
            Dim ActCommOleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)
            Dim ActCommSqlString As String = "SELECT * from NUM_COMMENTS_VIEW WHERE USER_UNAME=@f1"
            Dim ActCommCmd As OleDbCommand = New OleDbCommand(ActCommSqlString, ActCommOleDbConn)
            ActCommCmd.CommandType = CommandType.Text

            'Set the default value for ProfileStatsComments label for URL with no query string
            If (Request.Params("User") = "") Then
                lbl_ProfileStatsComments.Text = "0"
            Else
                'If there is a query string in the URL, read it and check if there is an entry in the db
                ActCommCmd.Parameters.AddWithValue("@f1", Request.Params("User"))
                ActCommOleDbConn.Open()
                Dim ActCommDr As OleDbDataReader = ActCommCmd.ExecuteReader
                If ActCommDr.HasRows Then
                    ActCommDr.Read()
                    If Not IsDBNull(ActCommDr("NumComments")) Then lbl_ProfileStatsComments.Text = ActCommDr("NumComments")
                Else
                    'If the user is not in the view (i.e. he never commented), then num of comments is zero
                    lbl_ProfileStatsComments.Text = "0"
                End If
            End If
            ActCommOleDbConn.Close()
            ActCommOleDbConn.Dispose()

            'Get the Recipe Activity Statistics
            Dim ActRecOleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)
            Dim ActRecSqlString As String = "SELECT * from NUM_RECIPE_VIEW WHERE USER_UNAME=@f1"
            Dim ActRecCmd As OleDbCommand = New OleDbCommand(ActRecSqlString, ActRecOleDbConn)
            ActRecCmd.CommandType = CommandType.Text

            'Set the default value for ProfileStatsComments label for URL with no query string
            If (Request.Params("User") = "") Then
                lbl_ProfileStatsRecipes.Text = "0"
            Else
                'If there is a query string in the URL, read it and check if there is an entry in the db
                ActRecCmd.Parameters.AddWithValue("@f1", Request.Params("User"))
                ActRecOleDbConn.Open()
                Dim ActRecDr As OleDbDataReader = ActRecCmd.ExecuteReader
                If ActRecDr.HasRows Then
                    ActRecDr.Read()
                    If Not IsDBNull(ActRecDr("NumRecipes")) Then lbl_ProfileStatsRecipes.Text = ActRecDr("NumRecipes")
                Else
                    'If the user is not in the view (i.e. he never commented), then num of comments is zero
                    lbl_ProfileStatsRecipes.Text = "0"
                End If
            End If
            ActRecOleDbConn.Close()
            ActRecOleDbConn.Dispose()


            'Get the Ratings Activity Statistics
            Dim RatRecOleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)
            Dim RatRecSqlString As String = "SELECT * from STAT_TOTAL_RATING_USER WHERE USER_UNAME=@f1"
            Dim RatRecCmd As OleDbCommand = New OleDbCommand(RatRecSqlString, RatRecOleDbConn)
            RatRecCmd.CommandType = CommandType.Text

            'Set the default value for ProfileStatsComments label for URL with no query string
            If (Request.Params("User") = "") Then
                lbl_ProfileStatsRating.Text = "0"
            Else
                'If there is a query string in the URL, read it and check if there is an entry in the db
                RatRecCmd.Parameters.AddWithValue("@f1", Request.Params("User"))
                RatRecOleDbConn.Open()
                Dim RatRecDr As OleDbDataReader = RatRecCmd.ExecuteReader
                If RatRecDr.HasRows Then
                    RatRecDr.Read()
                    If Not IsDBNull(RatRecDr("TtlUserRating")) Then lbl_ProfileStatsRating.Text = RatRecDr("TtlUserRating")
                Else
                    'If the user is not in the view (i.e. he never commented), then num of comments is zero
                    lbl_ProfileStatsRating.Text = "0"
                End If
            End If
            RatRecOleDbConn.Close()
            RatRecOleDbConn.Dispose()

        End If
    End Sub


    Protected Sub btn_PrSaveProfile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_PrSaveProfile.Click

        'Variable declaration separated from assignment because of if statements
        Dim newFileName As String = ""
        Dim SqlString As String

        'Checking if user has uploaded a new profile image
        If fup_PrPhoto.HasFile Then
            Dim myGUID As New System.Guid
            myGUID = Guid.NewGuid()
            newFileName = myGUID.ToString() + ".jpg"

            If fup_PrPhoto.HasFile Then
                fup_PrPhoto.SaveAs(Request.MapPath("Image") & "/" & newFileName)
            End If
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)

        'Depending on whether the user has uploaded a new profile image
        If fup_PrPhoto.HasFile Then
            'Option 1 - The user has uploaded a new profile image
            SqlString = "UPDATE PROFILE SET USER_FNAME=@f1,USER_LNAME=@f2,CTRY_ID=@f3,USER_DOB=@f4,USER_EMAIL=@f5,USER_BIO=@f6,USER_PHOTO=@f7 WHERE USER_UNAME=@f8"
        Else
            'Option 2 - The user has NOT uploaded a new profile image
            SqlString = "UPDATE PROFILE SET USER_FNAME=@f1,USER_LNAME=@f2,CTRY_ID=@f3,USER_DOB=@f4,USER_EMAIL=@f5,USER_BIO=@f6 WHERE USER_UNAME=@f8"
        End If

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_PrFName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_PrLName.Text)
        cmd.Parameters.AddWithValue("@f3", ddl_PrCountry.SelectedValue)
        cmd.Parameters.AddWithValue("@f4", tb_PrDOB.Text)
        cmd.Parameters.AddWithValue("@f5", tb_PrEmail.Text)
        cmd.Parameters.AddWithValue("@f6", tb_PrBio.Text)

        'If the user has uploaded a profile image
        If fup_PrPhoto.HasFile Then
            cmd.Parameters.AddWithValue("@f7", "Image/" & newFileName)
        End If

        cmd.Parameters.AddWithValue("@f8", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()

        'Clear everything from memory and close the db connection
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()

        Response.Redirect("Profile.aspx?User=" & User.Identity.Name)
    End Sub

End Class